package hero.mps.web.school_course.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import hero.mps.utils.ResultUtils;
import hero.mps.utils.ResultVo;
import hero.mps.web.course_teacher.entity.CourseTeacher;
import hero.mps.web.course_teacher.service.CourseTeacherService;
import hero.mps.web.school_course.entity.CourseParam;
import hero.mps.web.school_course.entity.SchoolCourse;
import hero.mps.web.school_course.service.SchoolCourseService;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/course")
@RequiredArgsConstructor
public class SchoolCourseController {
    private final SchoolCourseService schoolCourseService;
    private final CourseTeacherService courseTeacherService;

    @PostMapping
    public ResultVo addCourse(@RequestBody SchoolCourse schoolCourse) {
        boolean save = schoolCourseService.save(schoolCourse);
        if (save) {
            return ResultUtils.success("新增成功");
        }else {
            return ResultUtils.error("新增失败");
        }
    }

    @PutMapping
    public ResultVo editCourse(@RequestBody SchoolCourse schoolCourse) {
        boolean updateById = schoolCourseService.updateById(schoolCourse);
        if (updateById) {
            return ResultUtils.success("编辑成功");
        }else {
            return ResultUtils.error("编辑失败");
        }
    }

    @DeleteMapping("/{courserId}")
    public ResultVo deleteCourse(@PathVariable("courserId") Integer courserId) {
        boolean removeById = schoolCourseService.removeById(courserId);
        if (removeById) {
            return ResultUtils.success("删除成功");
        }else {
            return ResultUtils.error("删除失败");
        }
    }

//    @GetMapping("/list")
//    public ResultVo listCourse(CourseParam courseParam) {
////        构建查询条件
//        QueryWrapper<SchoolCourse> queryWrapper = new QueryWrapper<>();
//        if (StringUtils.isNotEmpty(courseParam.getCourseName())){
//            queryWrapper.lambda().like(SchoolCourse::getCourseName, courseParam.getCourseName());
//        }
////        构建分页条件
//        IPage<SchoolCourse> page = new Page(courseParam.getCurrentPage(), courseParam.getPageSize());
//        IPage<SchoolCourse> courseIPage = schoolCourseService.page(page, queryWrapper);
//        return ResultUtils.success("查询成功", courseIPage);
//    }

    @GetMapping("/list")
    public ResultVo listCourse(CourseParam courseParam) {
        IPage<SchoolCourse> courseIPage = schoolCourseService.getCoursePage(courseParam);
        return ResultUtils.success("查询成功", courseIPage);
    }

    @PostMapping("/assignSave")
    public ResultVo assignSave(@RequestBody CourseTeacher courseTeacher) {
//        判断课程是否已经分配老师
        QueryWrapper<CourseTeacher> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(CourseTeacher::getCourseId, courseTeacher.getCourseId());
        CourseTeacher one = courseTeacherService.getOne(queryWrapper);
        if (one != null) {
            return ResultUtils.error("该课程已分配老师，请勿重新分配");
        }
        boolean save = courseTeacherService.save(courseTeacher);
        if (save) {
            return ResultUtils.success("课程分配成功");
        }else {
            return ResultUtils.error("课程分配失败");
        }
    }

    @PostMapping("/removeTeacher")
    public ResultVo removeTeacher(@RequestBody CourseTeacher courseTeacher) {
        QueryWrapper<CourseTeacher> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(CourseTeacher::getCourseId, courseTeacher.getCourseId())
                .eq(CourseTeacher::getTeacherId, courseTeacher.getTeacherId());
        boolean remove = courseTeacherService.remove(queryWrapper);
        if (remove) {
            return ResultUtils.success("解除成功");
        }else {
            return ResultUtils.error("解除失败");
        }
    }
}
